package com.sy.controller;

import com.sy.bean.Receipt;
import com.sy.bean.pucharse.Purchase;
import com.sy.common.bean.PageBean;
import com.sy.common.bean.ResultBean;
import com.sy.common.controller.BaseController;
import com.sy.common.exception.ValidateException;
import com.sy.common.utils.BeanOfMap;
import com.sy.service.ReceiptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "付款单管理")
@RequestMapping("/api")
public class ReceiptController extends BaseController {
    @Autowired
    ReceiptService receiptService;

    @ApiOperation("保存付款单")
    @PostMapping("/receipt")
    public ResultBean save(@RequestBody Receipt receipt) {
        int rs = receiptService.save(receipt);
        return gotoResult(rs, "保存");
    }
    @ApiOperation("审核付款单")
    @PutMapping("/receipt/audit")
    public ResultBean audit(@RequestParam(value = "id", required = true)int id){
        int rs = receiptService.audit(id);
        return gotoResult(rs,"审核");
    }
    @ApiOperation("付款单列表")
    @PostMapping("/receipt/list")
    public ResultBean<Receipt> query(@RequestBody Receipt receipt) {
        PageBean pageBean = receiptService.query(BeanOfMap.bom(receipt));
        return success("付款单列表",pageBean);
    }
    @ApiOperation("导出付款单列表")
    @PostMapping("/receipt/export")
    public ResultBean exportRec(@RequestBody Map map) {
        String url = receiptService.exportRec(map);
        return success("导出付款单列表",url);
    }
    @ApiOperation("批量删除付款单")
    @DeleteMapping("/receipt/delete")
    public ResultBean delete(@RequestParam("ids") List<Integer> ids) {
        int rs = receiptService.batchDelete(ids);
        return rs>0?success("删除成功"):fail("删除失败");
    }
    @ApiOperation("付款单详情")
    @GetMapping("/receipt/get")
    public ResultBean get(int id){
        Receipt receipt = receiptService.get(id);
        return success("付款单详情",receipt);
    }
    @PutMapping("/receipt")
    @ApiOperation("修改付款单")
    public ResultBean update(@RequestBody Receipt receipt){
        if (null==receipt.getReceiptDetailList()||receipt.getReceiptDetailList().size()==0)
            throw new ValidateException("purchaseDetailList","付款明细不能为空!");
        receipt.getReceiptDetailList().forEach(o->{
            if (null==o.getAccount()||"".equals(o.getAccount()))
                throw new ValidateException("getAccount","账户名称不能为空!");
        });
        int rs = receiptService.update(receipt);
        return gotoResult(rs,"编辑");
    }
}
